<?php
/**
 *NewPage Controller
 *
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * Class NewPage_controller
 * sets up a new page
 *@package controller
 */

class NewPage_Controller extends Controller{

    /**
     * Holds the page handed over by the router
     * @var string
     */
    private $sPage = '';

    /**
     * Holds the page type handed over by the router default page type is contentPage
     * @var string
     */
    private $sPageType = "contentPage";

    /**
     * language in which to show page
     * @var integer
     */
    private $iLang ='';

    /**
     * Reference to view of content section
     * @var object
     */
    private $oContentView;

    /**
     * Reference to page structure model
     * @var object
     */
    private $oPageStructureModel;

    /**
     * Reference to NewPageModel to creat a new page
     * @var object
     */
    private $oNewPageModel;

    /**
     * Reference to model that retrieves page navigation elements from db
     * @var object
     */
    private $oNavigationModel;

    /**
     * Reference to view of navigation section
     * @var object
     */
    private $oNavigationView;

    /**
     * Reference to view of left navigation section in cms, it shows the entire pagestructure
     * @var object
     */
    private $oLeftNavigationView;

    /**
     * Error message which will be displayed in case of error
     * @var string
     */
    private $sError;


    public function __construct(){
        //Get Viewmanger reference from parent constructor
        parent::__construct();

        //Load models and views
        $this->oNewPageModel = Loader::loadModel('newPage');
        $this->oPageStructureModel = Loader::loadModel('pageStructure');
        $this->oNavigationModel = Loader::loadModel('navigation');

        //Load templates for navigation and leftnavigation section
        $this->oNavigationView = $this->oViewmanager->getView('navigation');
        $this->oLeftNavigationView = $this->oViewmanager->getView('leftnavigation');

        //Check language set in Cookie, take default language specified in config if not set
        if (isset($_COOKIE['language'])){
            $this->iLang = intval($_COOKIE['language']);
        }else{
            $this->iLang = DEFAULT_LANGUAGE;
        }
    }

        /**
     * checks GET-Parameters forwarded from Router::route
     * @param Array $aArgs Arguments to check
     */
    public function checkArgs($aArgs){
        //Get the argument for a function call
        $sArg = array_shift($aArgs);

        //Checks if category must be created
        if($sArg == 'category'){
            $this->sPageType = "category";
            $iProductPageID = array_shift($aArgs);
            $this->oContentView = $this->oViewmanager->getView('newCategory');
            $this->oContentView->assign('productPageID', $iProductPageID);
        }else{
            $this->oContentView = $this->oViewmanager->getView('newPage');
            if(isset($_GET['parentID'])){
                $iParentID = $_GET['parentID'];
                $this->oContentView->assign('parentID', $iParentID);
            }
            //Gets all possible parents to show in dropdown menu
            $this->oContentView->assign('parents', $this->oPageStructureModel->getPageStructure($this->iLang));
        }
        //Checks if a new page must be created
        if ($sArg == 'addNewPage'){
            if(isset($_POST['type'])){
                if($_POST['type'] == 1){
                    //Creates a product page
                    $this->sPageType = "product";
                }else{
                    //Creates a content page
                    $this->sPageType = "contentPage";
                }
            }else{
                //Sets default page type to contentPage
                $this->sPageType = "contentPage";
            }

            //Languge ID
            $iLid = $this->iLang;
            // If name, parent and visible is in the POST array
            if (isset($_POST['name'])&&isset($_POST['parent'])&&isset($_POST['visible'])){
                //Stores a possible return value
                $this->sError = $this->oNewPageModel->addPage($this->sPageType, $_POST['name'], $iLid, $_POST['parent'],$_POST['visible']);
                //Locates back to the pageStructure view
                header('Location: index.php?co=pageStructure');
                exit();
            }
            //Checks if a new category must be created
        }elseif($sArg == 'addNewCategory'){
            $this->sPageType = "category";
            $iLid = $this->iLang;
            // If name, parent and visible is in the POST array
            if (isset($_POST['name'])&&isset($_POST['parent'])&&isset($_POST['visible'])){
                //Stores a possible return value
                $this->sError = $this->oNewPageModel->addPage($this->sPageType, $_POST['name'], $iLid, $_POST['parent'],$_POST['visible']);
                //Locates back to the pageStructure view
                header('Location: index.php?co=product&pid='.$_POST['parent']);
                exit();
            }
        }

    }
/**
 * Executes controller methods
 * @param array different arguments
 */
    public function execute($aArgs){

        $this->checkArgs($aArgs);
        //Gets page data from database and passes data to views
        $this->oNavigationView->assign('contentANDproductPages', $this->oNavigationModel->getPageStructure('NULL', $this->iLang, "(type = 'contentPage' OR type = 'product')"));
        $this->oNavigationView->assign('products', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'product'));
        $this->oNavigationView->assign('categories', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'category'));
        $this->oLeftNavigationView->assign('leftnavigation', $this->oNavigationModel->getPageStructure('NULL', $this->iLang));
        //Insert data in the correct sections of the website layout
        $this->oViewmanager->setSection('Title', $this->oNewPageModel->getPageTitle($this->sPageType));
        $this->oViewmanager->setSection('Content', $this->oContentView->display());
        $this->oViewmanager->setSection('LeftNavigation', $this->oLeftNavigationView->display());
        $this->oViewmanager->setSection('Navigation', $this->oNavigationView->display());
    }
}

?>